package top.yangbuyi.service.impl;

import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import top.yangbuyi.domain.Scheduling;
import top.yangbuyi.domain.User;
import top.yangbuyi.dto.SchedulingFormDto;
import top.yangbuyi.dto.SchedulingQueryDto;
import top.yangbuyi.mapper.SchedulingMapper;
import top.yangbuyi.service.SchedulingService;
import top.yangbuyi.utils.StringUtils;

/**
 * @ClassName: SchedulingServiceImpl
 * @Author: campsis-tk
 * @Date: 2020/9/11 14:22
 * @Description:
 **/
@Service
public class SchedulingServiceImpl implements SchedulingService {

	@Autowired
	private SchedulingMapper schedulingMapper;

	@Override
	public List<Scheduling> queryScheduling(SchedulingQueryDto schedulingQueryDto) {
		QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
		wrapper.eq(null != schedulingQueryDto.getUserId(), User.COL_USER_ID, schedulingQueryDto.getUserId());
		wrapper.eq(null != schedulingQueryDto.getDeptId(), User.COL_DEPT_ID, schedulingQueryDto.getDeptId());
		wrapper.ge(StringUtils.isNotBlank(schedulingQueryDto.getBeginDate()), Scheduling.COL_SCHEDULING_DAY, schedulingQueryDto.getBeginDate());
		wrapper.le(StringUtils.isNotBlank(schedulingQueryDto.getEndDate()), Scheduling.COL_SCHEDULING_DAY, schedulingQueryDto.getEndDate());
		return this.schedulingMapper.selectList(wrapper);
	}

	@Override
	public int saveScheduling(SchedulingFormDto schedulingFormDto) {
		// 保存条件
		if (null != schedulingFormDto.getData() && schedulingFormDto.getData().size() > 0) {
			DateTime dateTime = DateUtil.parse(schedulingFormDto.getBeginDate(), "yyyy-MM-dd");
			// 得到beginDate所在周的周一
			DateTime date = DateUtil.beginOfWeek(dateTime);
			String beginDate = DateUtil.format(date, "yyyy-MM-dd");
			String endDate = DateUtil.format(DateUtil.endOfWeek(dateTime), "yyyy-MM-dd");
			// 得到用户名和科室
			SchedulingFormDto.SchedulingData schedulingData = schedulingFormDto.getData().get(0);
			Long userId = schedulingData.getUserId();
			Long deptId = schedulingData.getDeptId();
			if (null != userId) {
				// 删除原来的这个用户的当前周的所有排班数据
				QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
				wrapper.eq(Scheduling.COL_USER_ID, userId);
				wrapper.eq(Scheduling.COL_DEPT_ID, deptId);
				wrapper.between(Scheduling.COL_SCHEDULING_DAY, beginDate, endDate);
				this.schedulingMapper.delete(wrapper);
				// 在进行保存,添加新的排班
				// 初始化当前周一到周日的日期数据
				List<String> schedulingDays = initSchedulingDay(date);
				for (SchedulingFormDto.SchedulingData d : schedulingFormDto.getData()) {
					Scheduling scheduling;
					int i = 0;
					for (String s : d.getSchedulingType()) {
						if (StringUtils.isNotBlank(s)) {
							scheduling = new Scheduling(userId, deptId, schedulingDays.get(i), d.getSubsectionType(), s, new Date(), schedulingFormDto.getSimpleUser().getUserName());
							this.schedulingMapper.insert(scheduling);
						}
						i++;
					}
				}
				return 1;// 保存成功
			} else {
				return 0;// 用户为空,保存失败
			}
		}
		return 0;// 保存失败
	}

	/**
	* @Description: 养猪先个人网址:http://yangzhuxian.top
	* @Param:
	* @return:
	* @Author: Mr.zhou
	* @Date: 2020/9/15
	*/
	@Override
	public List<Long> queryDeptIdsForScheduling(Long deptId, String subsectionType, String schedulingType, String schedulingDay) {
        Wrapper<Scheduling> wrapper=new QueryWrapper<>();
        ((QueryWrapper<Scheduling>) wrapper).eq(null!=deptId,Scheduling.COL_DEPT_ID,deptId);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(subsectionType),Scheduling.COL_SUBSECTION_TYPE,subsectionType);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(schedulingType),Scheduling.COL_SCHEDULING_TYPE,schedulingType);
        ((QueryWrapper<Scheduling>) wrapper).eq(StringUtils.isNotBlank(schedulingDay),Scheduling.COL_SCHEDULING_DAY,schedulingDay);
        List<Scheduling> schedulings = this.schedulingMapper.selectList(wrapper);
        List<Long> longs=new ArrayList<>();
        longs.clear();
        for (Scheduling scheduling : schedulings) {
            longs.add(scheduling.getDeptId());
        }
//        List<Long> longs = this.schedulingMapper.queryDeptIdsForScheduling(deptId, subsectionType, schedulingType, schedulingDay);
        return longs;
	}

    @Override
    public List<Scheduling> selectAll2() {
        return schedulingMapper.selectAll2();
    }

    /**
     * 修改挂号状态为已收费
     * @param regId
     * @return
     */
    @Override
    public int saveCharge(String regId) {
        return 0;
    }

    /**
	 * @Description: 根据开始时间构造一周的日期数组
	 * @Param: beginTime
	 * @Return: List<String>
	 * @Author: campsis-tk
	 * @Exception:
	 * @Date: 2020/9/14
	 */
	private List<String> initSchedulingDay(Date beginTime) {
		List<String> list = new ArrayList<>();
		for (int i = 0; i < 7; i++) {
			DateTime dateTime = DateUtil.offsetDay(beginTime, i);// 偏移天数，正数向未来偏移，负数向历史偏移
			String key = DateUtil.format(dateTime, "yyyy-MM-dd");
			list.add(key);
		}
		return list;
	}
}
